package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Employee;
import com.lmb.bean.R;
import com.lmb.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /*
        //1.接收到用户登录输入的用户名和密码，封装到了emp对象中 对密码进行MD5加密
        //2.根据用户名查询数据库，返回Employee对象
        //3.判断Employee对象是否为null  null：员工不存在
        //4.比对密码是否一致           不一致：员工账号和密码不匹配
        //5.查询员工状态是否禁用        禁用 0：员工已禁用
        //6.登录成功，将员工id存入Session，返回成功结果【员工对象】
     */

    //登录
    @PostMapping("/login")
    public R login(HttpSession session, @RequestBody Employee emp){
        log.info("【登录员工信息：{}】",emp);
        String password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,emp.getUsername());
        Employee employee = employeeService.getOne(wrapper);
        if (employee==null){
            return R.error("员工不存在！");
        }
        if (password==null){
            return R.error("员工账号和密码不匹配！");
        }
        if (employee.getStatus()==0){
            return R.error("员工已禁用！");
        }
        session.setAttribute("employee",employee.getId());
        return R.success(employee);
    }

    //退出登录
    @PostMapping("/logout")
    public R logout(HttpSession  session){
        //1.清理session中用户信息
        //session.removeAttribute("employee");  //可以 不推荐  表示当前用户退出登录了 只清除了session中的一个数据
        session.invalidate();       //直接让当前这个用户关联的session对象立即失效  其中保存的数据也被删掉了
        //2.返回处理结果
        return R.success("退出成功！");
    }

    //新增员工
    @PostMapping
    public R add(HttpSession session,@RequestBody Employee emp){
        emp.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        emp.setStatus(1);

        //emp.setCreateTime(LocalDateTime.now());
        //emp.setUpdateTime(LocalDateTime.now());

        //Long employeeId = (Long) session.getAttribute("employee");
        //emp.setCreateUser(employeeId);
        //emp.setUpdateUser(employeeId);

        boolean flag = employeeService.save(emp);
        return flag?R.success(true):R.error("新增员工失败！");
    }

    //分页查询员工
    @GetMapping("/page")
    public R<Page<Employee>> selectPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize, String name){
        //1.调用业务处理
        Page<Employee> empPage = employeeService.findPage(page,pageSize,name);

        //2.响应处理结果
        return R.success(empPage);
    }

    /**
     * 启用禁用员工账号 | 编辑员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> updateEmp(@RequestBody Employee employee,HttpSession session){

        log.info("EmployeeController...："+Thread.currentThread().getId());
        //修改时 补全employee对象字段  update_user update_time
        //employee.setUpdateUser((Long) session.getAttribute("employee"));
        //employee.setUpdateTime(LocalDateTime.now());

        //1.调用service处理
        int rows = employeeService.updateEmp(employee);

        //2.响应处理结果
        return rows>0?R.success("修改成功！"):R.error("修改失败！");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //1.调用service处理  getById()
        Employee employee = employeeService.getById(id);

        //2.响应处理结果
        return R.success(employee);
    }

}
